from enum import Enum
from tkinter import Tk, filedialog
import cv2
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QApplication, QLabel

from my_model import predict
from data_pre_process import save_processed_image, predict_image_segmentation
from UI.MainWindow import Ui_MainWindow
import pathlib

class DenoisingAlgorithm(Enum):
    Gaussian = 1,
    Other = 99


class MW(Ui_MainWindow):
    def __init__(self):
        self.denoisingAlgorithm = DenoisingAlgorithm.Gaussian
        self.file_path = ''

    # def AddMyLabel(self):
    #     self.myLabel = MyLabel(self.groupBox)
    #     # self.myLabel.button_clicked_signal.connect(self.OpenFile)
    #     self.myLabel.setText("原图")
    #     self.myLabel.button_clicked_signal.connect(self.OpenFile)

    # ---------------myFunctions--------------------
    # ---------------添加相应函数---------------------
    def AddListeners(self):
        # self.actionOpen.triggered.connect(self.OpenFile)
        # self.actionClear.triggered.connect(self.Clear)
        self.AlgorithmComboBox.addItems(['Gaussian', 'Other'])
        self.AlgorithmComboBox.currentIndexChanged.connect(self.handleDenoisingAlgorithmChange)
        self.startBtn.clicked.connect(self.startRecognition)
        self.imageBtn.clicked.connect(self.OpenFile)
        self.OpenFileBtn.clicked.connect(self.OpenFile)
        self.ClearBtn.clicked.connect(self.Clear)
        self.CloseBtn.clicked.connect(self.CloseApp)
        self.MinimizeBtn.clicked.connect(self.MinimizeWindow)

        self.CharImageLabels = [self.resultImage_1, self.resultImage_2, self.resultImage_3,
                                self.resultImage_4, self.resultImage_5, self.resultImage_6]
        self.CharTextLabels = [self.resultText_1, self.resultText_2, self.resultText_3,
                                self.resultText_4, self.resultText_5, self.resultText_6]

    # ---------------功能函数---------------------

    # 关闭应用
    def CloseApp(self):
        QApplication.exit()

    def MinimizeWindow(self):
        QApplication.activeWindow().setWindowState(Qt.WindowMinimized)
        # QApplication.
        pass

    # 打开一个验证码文件
    def OpenFile(self):
        Tk().withdraw()
        self.file_path = filedialog.askopenfilename(title='选择文件', filetypes=[('JPG', '*.jpg'), ('All Files', '*')])
        data_root = pathlib.Path(self.file_path).parent
        name = pathlib.Path(self.file_path).relative_to(data_root)  # 计算相对路径
        my_image = cv2.imread(self.file_path)
        cv2.imwrite(r"C:\Users\14121\Desktop\predict\{}".format(name), my_image)
        self.image.setPixmap(self.file_path)
        save_processed_image(self.file_path, "result")
        self.ShowImage()
        self.ClearAllCharText()
        self.ClearAllCharImage()


    # 清空
    def Clear(self):
        self.file_path = ''
        self.ClearImage()
        self.ClearAllCharImage()
        self.ClearAllCharText()

    # 显示原图的 灰度图 去噪图 二值图
    def ShowImage(self):
        grayImagePath = self.file_path[:len(self.file_path)-4] + '/result/gray'
        binaryImagePath = self.file_path[:len(self.file_path)-4]  + '/result/binary'
        denoisingImagePath = self.file_path[:len(self.file_path)-4]  + '/result/denoise'

        print(grayImagePath,binaryImagePath,denoisingImagePath)

        self.grayImage.setPixmap(grayImagePath)
        self.binaryImage.setPixmap(binaryImagePath)
        self.denoisingImage.setPixmap(denoisingImagePath)

    # 清空原图的 灰度图 去噪图 二值图
    def ClearImage(self):
        self.image.clear()
        self.grayImage.clear()
        self.binaryImage.clear()
        self.denoisingImage.clear()

    # 显示每个字符的图片
    def ShowAllCharImage(self):
        data_root=pathlib.Path(r"D:\dataset\predict_segmented_image")
        i=0
        for item in data_root.iterdir():
            path = str(item)
            self.CharImageLabels[i].setPixmap(path)
            i+=1

    # 清空每个字符的图片
    def ClearAllCharImage(self):
        self.resultImage_1.clear()
        self.resultImage_2.clear()
        self.resultImage_3.clear()
        self.resultImage_4.clear()
        self.resultImage_5.clear()
        self.resultImage_6.clear()
        pass

    # 显示每个字符的值
    def ShowAllCharText(self,chars):
        i=0
        for item in chars:
            self.CharTextLabels[i].setText(item)
            i+=1
        pass

    # 清空每个字符的值
    def ClearAllCharText(self):
        self.resultText_1.clear()
        self.resultText_2.clear()
        self.resultText_3.clear()
        self.resultText_4.clear()
        self.resultText_5.clear()
        self.resultText_6.clear()

    # 处理去噪算法选择框内容改变
    def handleDenoisingAlgorithmChange(self):
        # 调用去噪算法
        denoisingImagePath = self.file_path[:len(self.file_path)-4] + '/denoising'
        self.denoisingImage.setPixmap(denoisingImagePath)

    # 开始识别
    def startRecognition(self):
        predict_image_segmentation()
        self.ShowAllCharImage()
        chars = predict()
        self.ShowAllCharText(chars)


    # 处理识别结果 显示单个字符图片和值
    def processResult(self):
        self.ShowAllCharImage()
        self.ShowAllCharText()

    def GetImageNameFromAbsPath(self, AbsPath):
        # imagePath = AbsPath.
        pass